package com.dm.cloud.file.conf;


import com.dm.cloud.core.exception.CustomException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.text.SimpleDateFormat;

@Component
@ConfigurationProperties(prefix = "dm.file.local")
@Data
public class LocalUtil {
    @Value("${server.port}")
    private Integer serverPort;

    @Value("${dm.client-ip}")
    private String clientIp;

    /**
     * 本地工作目录
     */
    private String workPath;

    /**
     * 资源目录，挂载的minio桶目录  equip-file
     */
    private String sourcePath;


    private static File workDir;
    /**
     * 资源目录，挂载的minio桶目录  equip-file
     */
    private static File sourceDir;

    /**
     * 分片缓存目录，桶目录二级
     */
    private static File burstDir;

    /**
     * 数据文件根目录，桶目录二级
     */
    private static File fileDir;

    /**
     * 算法管理插件目录，桶目录二级
     */
    private static File pluginDir;


    private final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");

    @PostConstruct
    public void init(){
        workDir=new File(workPath);
        if(!workDir.exists() || !workDir.isDirectory()){
            workDir.mkdirs();
        }
        sourceDir=new File(sourcePath);
        if(!sourceDir.exists() || !sourceDir.isDirectory()){
            throw new CustomException("资源目录不存在!");
        }
        fileDir = new File(sourceDir.getAbsolutePath() + File.separator + "fileDir");
        if(!fileDir.exists() || !fileDir.isDirectory()){
            fileDir.mkdirs();
        }
        burstDir = new File(sourceDir.getAbsolutePath() + File.separator + "burstWork");
        if(!burstDir.exists() || !burstDir.isDirectory()){
            burstDir.mkdirs();
        }
        pluginDir= new File(sourceDir.getAbsolutePath() + File.separator + "pluginDir");
        if(!pluginDir.exists() || !pluginDir.isDirectory()){
            pluginDir.mkdirs();
        }

    }

    /**
     * 获取本地工作目录
     * @return
     */
    public File getWorkDir(){
        return workDir;
    }

    /**
     * 获取资源根目录
     * @return
     */
    public File getSourceDir(){
        return sourceDir;
    }

    /**
     * 获取资源目录下的文件目录
     * @return
     */
    public File getFileDir(){
        return fileDir;
    }

    /**
     * 获取资源目录下的分片目录
     * @return
     */
    public File getBurstDir(){
        return burstDir;
    }

    public File getPluginDir(){
        return pluginDir;
    }

    /**
     * 获取资源目录下的分片目录下级目录
     * @param filePath
     * @return
     */
    public File getBurstDir(String filePath){
        return new File(burstDir.getAbsolutePath()+File.separator+filePath);
    }

    /**
     * 获取与发布目录的相对路径，返回结果 以 / 开头
     * @param file
     * @return
     */
    public String fileRelativePath(File file){
        return fileRelativePath(file.getAbsolutePath());
    }
    /**
     * 获取与发布目录的相对路径，返回结果 以 / 开头
     * @param fileAbs
     * @return
     */
    public String fileRelativePath(String fileAbs){
        fileAbs=fileAbs.replace("\\","/");
        String publicSourceAbs = fileDir.getAbsolutePath().replace("\\", "/");
        String relativePath = fileAbs.startsWith(publicSourceAbs)?fileAbs.substring(publicSourceAbs.length()):fileAbs;
        if(!relativePath.startsWith("/")){
            relativePath="/"+relativePath;
        }
        return relativePath;
    }

    public File getFileDir(String filePath) {
        return new File(fileDir.getAbsolutePath()+File.separator+filePath);
    }
}